package com.vito.oa.act.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.extend.model.OaActExBussDefModel;
import com.vito.oa.act.extend.model.OaActExBussModel;
import com.vito.oa.act.model.ActUserNodeSettingModel;
import com.vito.oa.act.model.ActUserProcessSettingModel;
import com.vito.oa.act.service.ActUserProcessSettingService;

@Controller
@RequestMapping("oa/act/userProcess")
public class ActUserProcessSettingAction {
	private static final Logger log = LoggerFactory.getLogger(ActUserProcessSettingAction.class);
	@Reference
	private ActUserProcessSettingService actUserProcessSettingService;
	
	@ResponseBody
	@RequestMapping(value = "queryUserProcessSetting")
	public Result queryUserProcessSetting(String key) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			r.setData(actUserProcessSettingService.queryUserProcessSetting(key, user.getDeptParentId()));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "updateUserProcessSetting")
	public Result updateUserProcessSetting(ActUserProcessSettingModel processSetting,String key) {
		Result r = new Result();
		try {
			if(Validata.isNullOrEmpty(key)){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(ResMsg.E31);
				return r;
			}
			UserModel user = SessionMap.getObj();
			OaActExBussModel process = new OaActExBussModel();
			process.setProcessKey(key);
			processSetting.setProcess(process);
			processSetting.setCompanyId(user.getDeptParentId());
			actUserProcessSettingService.updateUserProcessSetting(processSetting);
			r.setCode(Result.CODE_OK);
			r.setMsg(ResMsg.UPDSUC);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "queryUserNodesSettingByPage")
	public Result queryUserNodesSettingByPage(String key) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			ActUserProcessSettingModel setting = new ActUserProcessSettingModel();
			setting.setCompanyId(user.getDeptParentId());
			OaActExBussModel process = new OaActExBussModel();
			process.setProcessKey(key);
			setting.setProcess(process);
			r.setData(actUserProcessSettingService.queryUserNodesSettingByPage(setting));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "updateUserNodeSetting")
	public Result updateUserNodeSetting(ActUserNodeSettingModel nodeSetting,String key,String nodeId) {
		Result r = new Result();
		try {
			if(Validata.isNullOrEmpty(key) || Validata.isNullOrEmpty(nodeId)){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(ResMsg.E31);
				return r;
			}
			UserModel user = SessionMap.getObj();
			nodeSetting.setCompanyId(user.getDeptParentId());
			OaActExBussDefModel node = new OaActExBussDefModel();
			node.setProcessKey(key);
			node.setProcessNodeId(nodeId);
			nodeSetting.setNode(node);
			actUserProcessSettingService.updateNodesSetting(nodeSetting);
			r.setCode(Result.CODE_OK);
			r.setMsg(ResMsg.UPDSUC);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}
	
	@ResponseBody
	@RequestMapping(value = "queryAllSetting")
	public Result queryAllSetting(String key) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			r.setData(actUserProcessSettingService.queryProcessAndNodeSetting(key, user.getDeptParentId()));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
		}
		return r;
	}

}
